package com.itheima.service.impl;

import com.alicp.jetcache.anno.*;
import com.itheima.dao.BookDao;
import com.itheima.domain.Book;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * className: BookServiceImpl
 * description:
 * date: 2023/3/9 0009-PM 11:13:34
 * <p>
 * project: springboot
 * package: com.itheima.service.impl
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao bookDao;

    /**
     * 保存
     *
     * @param book {@link Book}
     * @return {@link Boolean}
     */
    @Override
    public boolean save(Book book) {
        return bookDao.insert(book) > 0;
    }

    /**
     * 根据 id 获取
     *
     * @param id {@link Integer}
     * @return {@link Book}
     */
    @Override
    @Cached(name = "book_", key = "#id", expire = 3600, cacheType = CacheType.REMOTE)
    // @CacheRefresh(refresh = 5)
    public Book getById(Integer id) {
        return bookDao.selectById(id);
    }

    /**
     * 更新
     *
     * @param book {@link Book}
     * @return {@link Boolean}
     */
    @Override
    @CacheUpdate(name = "book_", key = "#book.id", value = "#book")
    public boolean update(Book book) {
        return bookDao.updateById(book) > 0;
    }

    /**
     * 根据 id 删除
     *
     * @param id {@link Integer}
     * @return {@link Boolean}
     */
    @Override
    @CacheInvalidate(name = "book_", key = "#id")
    public boolean delete(Integer id) {
        return bookDao.deleteById(id) > 0;
    }

    /**
     * 查询所有
     *
     * @return {@link List <Book>}
     */
    @Override
    public List<Book> getAll() {
        return bookDao.selectList(null);
    }
}
